<?php

/**
 * This is the model class for table "curso_ciclo".
 *
 * The followings are the available columns in table 'curso_ciclo':
 * @property integer $id
 * @property integer $id_ciclo
 * @property integer $id_curso
 * @property integer $id_seccion
 * @property string $usuario_ing
 * @property string $creado
 * @property string $usuario_act
 * @property string $fecha_act
 *
 * The followings are the available model relations:
 * @property Ciclo $idCiclo
 * @property Curso $idCurso
 * @property Seccion $idSeccion
 * @property Horario[] $horarios
 * @property RolPartido[] $rolPartidos
 */
class CursoCiclo extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CursoCiclo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'curso_ciclo';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_ciclo, id_curso, id_seccion', 'required'),
			array('id_ciclo, id_curso, id_seccion', 'numerical', 'integerOnly'=>true),
			array('usuario_ing, usuario_act', 'length', 'max'=>45),
			array('fecha_act', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, id_ciclo, id_curso, id_seccion, usuario_ing, creado, usuario_act, fecha_act', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idCiclo' => array(self::BELONGS_TO, 'Ciclo', 'id_ciclo'),
			'idCurso' => array(self::BELONGS_TO, 'Curso', 'id_curso'),
			'idSeccion' => array(self::BELONGS_TO, 'Seccion', 'id_seccion'),
			'horarios' => array(self::HAS_MANY, 'Horario', 'curso_ciclo_id'),
			'rolPartidos' => array(self::MANY_MANY, 'RolPartido', 'participante_curso(id_curso_ciclo, id_rol_partido)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_ciclo' => 'Id Ciclo',
			'id_curso' => 'Id Curso',
			'id_seccion' => 'Id Seccion',
			'usuario_ing' => 'Usuario Ing',
			'creado' => 'Creado',
			'usuario_act' => 'Usuario Act',
			'fecha_act' => 'Fecha Act',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('id_ciclo',$this->id_ciclo);
		$criteria->compare('id_curso',$this->id_curso);
		$criteria->compare('id_seccion',$this->id_seccion);
		$criteria->compare('usuario_ing',$this->usuario_ing,true);
		$criteria->compare('creado',$this->creado,true);
		$criteria->compare('usuario_act',$this->usuario_act,true);
		$criteria->compare('fecha_act',$this->fecha_act,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function asignarParticipante($id_rol_partido){
	   $participanteCurso=new ParticipanteCurso();
	   $participanteCurso->id_curso_ciclo=$this->id;
	   $participanteCurso->id_rol_partido=$id_rol_partido;
	}
	
	public function beforeSave() {
		if ($this->isNewRecord){
			$this->creado = new CDbExpression('NOW()');
		}
		return parent::beforeSave();
	}
}